//index.js
//获取应用实例
const util = require('../../utils/util.js')
import Bmob from '../../utils/dist/Bmob-1.7.0.min.js'
const app = getApp()

Page({
  data: {
    shuru1: null,
    //ztsj:null,
    list: [],
    nu: 20,
    motto: 'Hello World',
    isLoading: false,//正在加载中
    noMore: false//是否还有更多数据
    
  },
  //事件处理函数
  upper: function (e) {
    console.log(e)
  },
  lower: function (e) {
    console.log(e)
  },
  scroll: function (e) {
    console.log(e)
  },
  onReachBottom: function () {
    if (!this.data.noMore) {
      var that = this;
      console.log('circle 下一页');
      this.setData({
        isLoading: true
      })
      var timer = setTimeout(function () {
        console.log(888);
        that.setData({
          isLoading: false
        })
        clearTimeout(timer);
      }, 1000)
    }
  },
  onLoad: function (options) {
    /* var data1 = []
    const query = Bmob.Query("recommendeddata");
    query.order("-createdAt");
    query.limit(20);
    query.find().then(res => {
      console.log(res)
      for (var i = 4; i < res.length; i++) {
        data1.push({
          menuid: res[i].dataid,
          pic: res[i].pic,
          name: res[i].name,
          peoplenum: res[i].people,
          preparetime: res[i].time
        })
      }
      var data3 = res.slice(0, 4)           //创建幻灯片内容
      var swiper = new Object();
      swiper.data3 = data3;
      swiper.userInfo = {};
      swiper.indicatorDots = true;
      swiper.indicatorColor = 'rgba(249,245,236,0.6)';
      swiper.indicatorActiveColor = '#FFCC66';
      swiper.autoplay = true;
      swiper.interval = 5000;
      swiper.duration = 500;
      swiper.circular = true;
      console.log(swiper)
      this.setData({
        swiper: swiper
      })
      console.log(data3)
      console.log(data1)
      this.setData({
        data2: data1
      })
    }).catch(err => {
      console.log(err)
    }) */
   
  },
  //页面跳转
  tiaozhuan:function(e){
    let item = e.currentTarget.dataset.item;
    let _str = JSON.stringify(item)

    wx.navigateTo({
      //url: e.currentTarget.dataset.path,
      url: '../search2/search2?shuju=' + _str,
    })


  },

  onShow: function(){
    var data1 = []
    const query = Bmob.Query("recommendeddata");
    query.order("-createdAt");
    query.limit(20);
    query.find().then(res => {
      //console.log(res,"数据")
      /* for (var i = 4; i < res.length; i++) {
        data1.push({
          id:res[i].objectId,
          menuid: res[i].dataid,
          pic: res[i].pic,
          name: res[i].name,
          peoplenum: res[i].people,
          preparetime: res[i].time
        })
      } */
      this.setData({
        data2:res
      })
      

      var data3 = res.slice(0, 4)           //创建幻灯片内容
      var swiper = new Object();
      swiper.data3 = data3;
      swiper.userInfo = {};
      swiper.indicatorDots = true;
      swiper.indicatorColor = 'rgba(249,245,236,0.6)';
      swiper.indicatorActiveColor = '#FFCC66';
      swiper.autoplay = true;
      swiper.interval = 5000;
      swiper.duration = 500;
      swiper.circular = true;
      //console.log(swiper)
      this.setData({
        swiper: swiper
      })
      //console.log(data3)
      //console.log(data1)
      /* this.setData({
        data2: data1
      }) */
    }).catch(err => {
      console.log(err)
    })

  },
  onReachBottom: function () {

    var data1 = []
    var nu = this.data.nu
    const query = Bmob.Query("recommendeddata");
    query.order("-createdAt");
    query.limit(20);
    query.skip(nu);
    query.find().then(res => {
      console.log(res)
      //console.log(res.length, "2")
      if (res.length == 0) {     //没有更多数据
        //console.log("1")
        this.setData({
          noMore: true
        })
      } else {
        this.setData({
          isLoading: true
        })
        //console.log(res, "5")
         for (var i = 0; i < res.length; i++) {
          data1.push({
            classid:res[i].classid,
            content:res[i].content,
            createdAt:res[i].createdAt,
            dataid:res[i].dataid,
            material:res[i].material,
            name:res[i].name,
            objectId:res[i].objectId,
            people:res[i].people,
            pic:res[i].pic,
            process:res[i].process,
            tag:res[i].tag,
            time:res[i].time,
            updatedAt:res[i].updatedAt,

          })
        } 
       
        var arr1 = this.data.data2;
        //console.log(arr1);
        var arr2 = data1;
        //console.log(arr2);
        arr1 = arr1.concat(arr2);
        //console.log(arr1)
        this.setData({
          data2: arr1

        })
       
      }
    }).catch(err => {
      console.log(err)
    })
    this.setData({
      nu: nu + 20
    })
  },
  /**
  * 生命周期函数--监听页面隐藏
  */
  onHide: function () {
     this.setData({
      nu:20
    }) 

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {


  }
  
})
